/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Order} from '../order/order-model.js';
var order = new Order();

Page({
    data: {
        pageIndex: 1,
        loadingHidden: false,
        restUrl: order.getUrl(),
        isLoadedAll: false,
        orderArr: [],
        state: 0
    },
    /**
     * 获取数据 状态
     */
    onLoad: function(option) {
        this.setData({
            state: option.state
        })
        this._getOrders();
    },
    /**
     * 获取订单数据
     */
    _getOrders: function(callback) {
        var that = this,
            param = {};
            param['page'] = this.data.pageIndex;
            param['state_type'] = this.data.state;
            param['client'] = 'min';
        order.getOrders(param, (data)=>{
            for (var i in data) {
                var list = data[i].orderItems;
                for (var j in list) {
                    list[j].price = list[j].price.toFixed(2);
                }
            }
            that.setData({
                loadingHidden: true
            })
            // 订单下拉,追加到[]中,显示追加效果,数组合并
            if (data.length > 0) {
                that.data.orderArr.push.apply(that.data.orderArr, data);
                that.setData({
                    orderArr: that.data.orderArr
                })
            } else {
                // 最后无数据,显示加载完毕
                that.setData({
                    isLoadedAll: true
                })
                // 加载完page重置1
                // that.data.pageIndex = 1;
            }
            callback && callback();
        })
    },
    /**
     * 监听用户下拉动作  重新刷新用户订单,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        this.data.orderArr = [];  // 初始化
        that.data.pageIndex = 1;
        this._getOrders(() => {
            that.setData({
                isLoadedAll: false
            })
            wx.stopPullDownRefresh();
            order.execSetStorageSync(false);  // 更新标志位 上已加,再加过滤中间发送订单情况
        });
    },
    /**
     * 页面上拉触底事件的处理函数  上拉到底部触发 &page=1++
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll) {
            this.data.pageIndex++;
            this._getOrders();
        }
    },
    /**
     * 显示订单信息
     */
    showOrderDetailInfo: function(event) {
        var id = order.getDataSet(event,'id');
        wx.navigateTo({
            url:'../order/order?from=order&id='+id
        });
    },
    /**
     * 未支付订单,再次支付
     */
    rePay: function(event) {
        var id = order.getDataSet(event,'id'), // 订单id
            index = order.getDataSet(event,'index'); // 订单在[]索引位置
        // 支付状态
        if(order.onPay) {
            this._execPay(id, index);
        }else {
            this.showTips('支付提示','本产品仅用于演示，支付系统已屏蔽');
        }
    },
    /**
     * 支付
     */
    _execPay: function(id, index) {
        var that = this;
        order.execPay(id, false, (statusCode)=>{
            if(statusCode>0){
                var flag = statusCode == 2; // 已支付
                // 更新订单显示状态
                if(flag){
                    that.data.orderArr[index].status = 2;
                    that.setData({
                        orderArr: that.data.orderArr
                    });
                }
                // 跳转到成功页面
                wx.navigateTo({
                    url: '../pay-result/pay-result?id='+id+'&flag='+flag+'&from=my'
                });
            }else{
                that.showTips('支付失败','商品已下架或库存不足');
            }
        });
    },
    /**
     * 提示窗口
     */
    showTips: function(title, content) {
        wx.showModal({
            title: title,
            content: content,
            showCancel: false,
            success: function(res) {
            }
        })
    }
})